/*
 * @Author: gggg 1263457581@qq.com
 * @Date: 2025-06-10 14:38:49
 * @LastEditors: gggg 1263457581@qq.com
 * @LastEditTime: 2025-06-10 14:57:43
 * @FilePath: /algorithm-and-data-struct/pool/EventLoop.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <condition_variable>
#include <functional>
#include <thread>
#include <vector>
#include <iostream>

class EventLoop {
public:
    using Callable_t = std::function<void()>;  
    EventLoop();
    ~EventLoop();
    void run();
    void stop();
    void addTask(std::function<void()> task);
private:
    std::thread m_thread;
    std::vector<std::function<void()>> m_tasks;
    std::mutex m_mutex;
    std::condition_variable m_condition;
    bool m_running;
};

EventLoop::EventLoop()
    : m_thread(&EventLoop::run, this)     //调用方法为：成员函数的地址，对象地址， 成员函数参数
    , m_running(true)
{
}

EventLoop::~EventLoop()
{
    stop();
    m_thread.join();
}

EventLoop::run()
{ 
    
}

EventLoop:addTask(Callable_t task)
{
    {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_tasks.emplace_back(std::move(task));
    }
    m_condition.notify_one();
}
